package org.pentaho.di.ui.core.dialog;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.pentaho.di.core.Const;
import org.pentaho.di.ui.core.FormDataBuilder;
import org.pentaho.di.ui.core.PropsUI;
import org.pentaho.di.ui.core.gui.GUIResource;
import org.pentaho.di.ui.core.gui.WindowProperty;

public abstract class BaseDialog extends Dialog {
    public static final int MARGIN_SIZE = 15;
    public static final int LABEL_SPACING = 5;
    public static final int ELEMENT_SPACING = 10;
    public static final int MEDIUM_FIELD = 250;
    public static final int MEDIUM_SMALL_FIELD = 150;
    public static final int SMALL_FIELD = 50;
    public static final int SHELL_WIDTH_OFFSET = 16;
    public static final int VAR_ICON_WIDTH;
    public static final int VAR_ICON_HEIGHT;
    protected Map<String, Listener> buttons;
    protected Shell shell;
    protected PropsUI props;
    protected int width;
    protected String title;
    private int footerTopPadding;

    public BaseDialog(Shell shell) {
        this(shell, (String) null, -1);
    }

    public BaseDialog(Shell shell, String title, int width) {
        super(shell, 0);
        this.buttons = new HashMap();
        this.width = -1;
        this.footerTopPadding = 40;
        this.props = PropsUI.getInstance();
        this.title = title;
        this.width = width;
    }

    private Display prepareLayout() {
        Shell parent = this.getParent();
        Display display = parent.getDisplay();
        this.shell = new Shell(parent, 268503136);
        this.shell.setImage(GUIResource.getInstance().getImageSpoon());
        this.props.setLook(this.shell);
        this.shell.addShellListener(new ShellAdapter() {
            public void shellClosed(ShellEvent e) {
                BaseDialog.this.dispose();
            }
        });
        FormLayout formLayout = new FormLayout();
        formLayout.marginWidth = 15;
        formLayout.marginHeight = 15;
        this.shell.setLayout(formLayout);
        this.shell.setText(this.title);
        return display;
    }

    protected abstract Control buildBody();

    public int open() {
        Display display = this.prepareLayout();
        Control lastBodyElement = this.buildBody();
        this.buildFooter(lastBodyElement);
        this.open(display);
        return 1;
    }

    private void open(Display display) {
        this.shell.pack();
        if (this.width > 0) {
            int height = this.shell.computeSize(this.width, -1).y;
            this.shell.setMinimumSize(this.width + 16, height);
            this.shell.setSize(this.width + 16, height);
        }

        this.shell.open();

        while (!this.shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }

    }

    protected void buildFooter(Control anchorElement) {
        Button[] buttonArr = new Button[this.buttons == null ? 0 : this.buttons.size()];
        int index = 0;
        Button button;
        if (this.buttons != null) {
            for (Iterator var4 = this.buttons.keySet().iterator(); var4.hasNext(); buttonArr[index++] = button) {
                String buttonName = (String) var4.next();
                button = new Button(this.shell, 8);
                button.setText(buttonName);
                Listener listener = (Listener) this.buttons.get(buttonName);
                if (listener != null) {
                    button.addListener(13, listener);
                } else {
                    button.addListener(13, (event) -> {
                        this.dispose();
                    });
                }
            }
        }

        Button previousButton = null;

        for (int i = buttonArr.length - 1; i >= 0; --i) {
            button = buttonArr[i];
            if (previousButton == null) {
                button.setLayoutData((new FormDataBuilder()).top(anchorElement, this.footerTopPadding).right(100, 0).result());
            } else {
                button.setLayoutData((new FormDataBuilder()).top(anchorElement, this.footerTopPadding).right(previousButton, Const.isOSX() ? 0 : -5).result());
            }

            previousButton = button;
        }

    }

    public void setFooterTopPadding(int footerTopPadding) {
        this.footerTopPadding = footerTopPadding;
    }

    public void dispose() {
        this.props.setScreen(new WindowProperty(this.shell));
        this.shell.dispose();
    }

    public void setButtons(Map<String, Listener> buttons) {
        this.buttons = buttons;
    }

    static {
        VAR_ICON_WIDTH = GUIResource.getInstance().getImageVariable().getBounds().width;
        VAR_ICON_HEIGHT = GUIResource.getInstance().getImageVariable().getBounds().height;
    }
}

